﻿using System.Collections;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Model;
using BLL;
using System.Web.Services;

namespace GeniusWebApplication
{
    public partial class item_media : System.Web.UI.Page
    {
        public static User user = null;
        public static int resourceId = 1;
        public static string userName = "";
        public string player_href = "";
        public static Resource resource = null;

        protected void Page_Load(object sender, EventArgs e)
        {
            resource = getResource();
            player_href = urlconvertor(resource.physicalPath);
            if (resource == null)
                Response.Redirect("index.aspx");

            user = (User)HttpContext.Current.Session["user"];

            if (user == null)
            { 
                Response.Redirect("log.aspx"); 
            }
            else
            {
                userName = user.name;
            }
            this.mark_H.InnerHtml = "历史评价：" + resource.marks / resource.marksUserNum + " 分";
            this.downTime.InnerHtml = "下载 " + resource.downloadTimes + " 次";
            this.dB.InnerHtml = "<a href='./download.aspx?resourceId=" + resource.id + "'><img src='./img/dB.png'></a>";
            if (resource.uploader.id != user.id)
            {
                this.the__uploader.InnerHtml = "<a href='./self.aspx?toname=" + resource.uploader.id + "&methodId=3' id='share_p'>共享者 ：" + resource.uploader.name + "</a>";
            }
            else
            {
                this.the__uploader.InnerHtml = "<a href='./self.aspx?toname=" + resource.uploader.id + "&methodId=1' id='share_p'>共享者 ：" + resource.uploader.name + "</a>";

            }
            this.media.InnerHtml = "<a href=" + player_href + " style='display:block;width:680px;height:330px' id='player'></a><script>flowplayer('player','./flowplayer/flowplayer-3.2.12.swf');</script>";

            //初始化评论
            IList<Comment> ccl = new ResourceService().getResourceComment(resource.id);
            foreach (Comment jk in ccl)
            {
                this.comm_con_1.InnerHtml = this.comm_con_1.InnerHtml + "<div class='comm_list'><p class='comm_user'>" + jk.user.name + ":" + jk.content + "</p><div class='comm_date'>" + jk.commentTime + "</div></div>";
            }

            //初始化关键字
            IList<Keyword> kl = new KeywordService().getKeywords();
            List<string> keyStringList = new List<string>();
            List<int> keyStringListBig = new List<int>();
            foreach (Keyword k in kl)
            {

                if (keyStringList.Count() == 0)
                {
                    keyStringList.Add(k.keywordName);
                    keyStringListBig.Add(5);
                }
                else if (keyStringList.FindIndex(delegate(string s) { return s == k.keywordName; }) == -1)
                {
                    keyStringList.Add(k.keywordName);
                    keyStringListBig.Add(5);
                }
                else
                {
                    int ii = keyStringList.FindIndex(delegate(string s) { return s == k.keywordName; });
                    if (keyStringListBig[ii] >= 2)
                    {
                        keyStringListBig[ii] = keyStringListBig[ii] - 1;
                    }
                }
            }

            for (int m = 0; m < keyStringList.Count(); m++)
            {
                this.keys.InnerHtml = this.keys.InnerHtml + "<div class='keysItem'><p class='ggz" + keyStringListBig[m] + "'><a href='./keywords.aspx?keys=" + keyStringList[m] + "'>" + keyStringList[m] + "</a></p></div>";
            }
        }
        private Resource getResource()
        {
            string id = Request.QueryString["resourceId"];
            if (id == null)
            {
                Response.Redirect("index.aspx");
                return null;
            }
            else
            {

                try
                {
                    resourceId = int.Parse(id);
                }
                catch
                {
                    Response.Redirect("index.aspx");
                    return null;
                }
                ResourceService rs = new ResourceService();
                Resource resource = rs.getResourceById(resourceId);
                return resource;
            }
        }

        [WebMethod]
        public static string ajaxSaveComment(string comment)
        {
            ResourceService resourceService = new ResourceService();
            Hashtable ht = resourceService.sendComment(user, resourceId, comment);
            string result = (string)ht["result"];
            string message = (string)ht["message"];

            return "{" +
                        "result:'" + result + "'," +
                        "message:'" + message + "'" +
                    "}";
        }

        [WebMethod]
        public static string ajaxSaveMark(int mark)
        {
            ResourceService resourceService = new ResourceService();
            Hashtable ht = resourceService.markResource(resource.id, mark);
            string result = (string)ht["result"];
            string message = (string)ht["message"];


            return "{" +
                        "result:'" + result + "'," +
                        "message:'" + message + "'" +
                    "}";
        }

        private string urlconvertor(string fileDIr)
        {
            string tmpRootDir = Server.MapPath(System.Web.HttpContext.Current.Request.ApplicationPath.ToString());//获取程序根目录
            string fielUrl = fileDIr.Replace(tmpRootDir, ""); //转换成相对路径
            fielUrl = fielUrl.Replace(@"\", @"/");
            return fielUrl;
        }
    }
}